var tuiwa = require("../../tuiwa.js");
const app = getApp()

Page({
  data: {
    swiper: {
      imgUrls: [
        // {
        //   images: 'https://fcdn.qiushilaile.com/adx/adx-24341611043850808.png',
        //   url: '/pages/packet-rain/packet-rain'
        // },
        {
          images: 'https://fcdn.qiushilaile.com/adx/adx-71391611044233845.png',
          url: '/pages/duilian/duilian'
        },
        {
          images: 'https://fcdn.qiushilaile.com/adx/app-13191611745552932.png',
          url: '/pages/collectFiveBlessings/collectFiveBlessings'
        },
        {
          images: 'https://fcdn.qiushilaile.com/adx/adx-40721611043828854.png',
          url: '/pages/newYearCard/newYearCard'
        },
        {
          images: 'https://fcdn.qiushilaile.com/adx/adx-96361611043860797.png',
          url: '/pages/dengmi/denglong/denglong'
        },
        {
          images: 'https://fcdn.qiushilaile.com/adx/adx-461611043842768.png',
          url: '/pages/newYearFlag-one/newYearFlag-one'
        },
        // {
        //   images: 'https://fcdn.qiushilaile.com/adx/adx-99721611137881669.png',
        //   url: '/pages/2021Test/2021Test'
        // },
        {
          images: 'https://fcdn.qiushilaile.com/adx/app-47041612526850254.png',
          url: '/pages/chengyu/cycontent/cycontent'
        },

      ],
      current: 0,
    },
    widthProgress: 0,
    testLoadingLayer: true,
    lead: false,
    currentIndex: 0,
    onTime: 1,
    jssj: "2021-02-12 00:00:00", // 设置结束时间
    // timer: "", // 倒计时定时器名称
    // qgdjs_jo: { day: "00", hour: "00", min: "00", sec: "00" }, // 倒数计时
  },
  onLoad: function () {
    setTimeout(() => {
      this.setData({
        testLoadingLayer: false
      })
    }, 4000)
    setTimeout(() => {
      this.setData({
        lead: false
      })
    }, 7000)
    let openId = tt.getStorageSync('openId')
    let flag = tt.getStorageSync('flag')
    if (flag) {
      console.log('flag存在', flag)
    } else {
      console.log('flag不存在')
      this.setData({
        lead: true
      })
      tt.setStorageSync('flag', openId);
    }
    let options = tt.getStorageSync('options');
    if (options) {
      if (options.query) {
        if (options.query.goodsId) {
          let allGoodsId = options.query.goodsId.split("_")
          tt.setStorageSync('g_id', options.query.goodsId);
          // tuiwa.getAdEvent("getAd");
          let goodsId = "";
          let subGoodsId = "";
          if (allGoodsId.length > 1) {
            subGoodsId = allGoodsId[1]
            tt.setStorageSync('subGoodsId', subGoodsId);
          } else {
            subGoodsId = undefined;
          }
          goodsId = allGoodsId[0];
          if (goodsId == "hongbaoyu") {
            tt.navigateTo({
              url: '/pages/packet-rain/packet-rain' //  跳转到红包雨
            });
          }
          if (goodsId == "heka") {
            tt.navigateTo({
              url: '/pages/newYearCard/newYearCard' //跳转到贺卡
            });
          }
          if (goodsId == "duilian") {
            tt.navigateTo({
              url: '/pages/duilian/duilian' // 对联
            });
          }
          if (goodsId == "dengmi") {
            tt.navigateTo({
              url: '/pages/dengmi/denglong/denglong' // 灯谜
            });
          }
          if (goodsId == "flag") {
            tt.navigateTo({
              url: '/pages/newYearFlag/newYearFlag' // 新年flag
            });
          }
          if (goodsId == "ceshi") {
            tt.navigateTo({
              url: '/pages/2021Test/2021Test' // 测试
            });
          }
          if (goodsId == "chengyu") {
            tt.navigateTo({
              url: '/pages/chengyu/cycontent/cycontent' //  成语
            });
          }
        }
      }
    }
    this.countDown();
  },
  handleChange: function (e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
  onJump: function (e) {
    var url = e.target.dataset.url
    tt.navigateTo({
      url: url,
    })
  },
  // 左切换按钮
  prevImg: function () {
    // 获取数据(方便下面做逻辑处理)
    var swiper = this.data.swiper;
    var current = swiper.current;
    // 逻辑判断(判断指示器索引)
    swiper.current = current > 0 ? current - 1 : swiper.imgUrls.length - 1;
    // 更新数据
    this.setData({
      swiper: swiper,
    })
  },
  // 右切换按钮
  nextImg: function () {
    // 获取数据(方便下面做逻辑处理)
    var swiper = this.data.swiper;
    var current = swiper.current;
    // 逻辑判断(判断指示器索引)
    swiper.current = current < (swiper.imgUrls.length - 1) ? current + 1 : 0;
    // 更新数据
    this.setData({
      swiper: swiper,
    })
  },
  // 新年倒计时
  countDown: function () {
    let that = this;
    that.setData({
      timer: setInterval(function () {
        var lefttime = parseInt((new Date(that.data.jssj.replace(/-/g, "/")).getTime() - new Date().getTime()));
        if (lefttime <= 0) {
          that.setData({
            qgdjs_jo: { day: "00", hour: "00", min: "00", sec: "00" },
            onTime: 0,
          })
          clearInterval(that.data.timer);
          if (that.data.onTime == 0) {
            that.countUp();
          }
          return;
        }
        var d = parseInt(lefttime / 1000 / 3600 / 24); //天数
        var h = parseInt(lefttime / 1000 / 3600 % 24); //小时
        var m = parseInt(lefttime / 1000 / 60 % 60);   //分钟
        var s = parseInt(lefttime / 1000 % 60);        //秒
        d < 10 ? d = "0" + d : d;
        h < 10 ? h = "0" + h : h;
        m < 10 ? m = "0" + m : m;
        s < 10 ? s = "0" + s : s;
        that.setData({
          qgdjs_jo: { day: d, hour: h, min: m, sec: s }
        })
      }, 1000)
    })

  },
  countUp: function () {
    // console.log('1111')
    let that = this
    this.setData({
      timer: setInterval(() => {
        var righttime = parseInt((new Date().getTime()) - new Date(that.data.jssj.replace(/-/g, "/")).getTime());
        var d = parseInt(righttime / 1000 / 3600 / 24); //天数
        var h = parseInt(righttime / 1000 / 3600 % 24); //小时
        var m = parseInt(righttime / 1000 / 60 % 60);   //分钟
        var s = parseInt(righttime / 1000 % 60);        //秒
        d < 10 ? d = "0" + d : d;
        h < 10 ? h = "0" + h : h;
        m < 10 ? m = "0" + m : m;
        s < 10 ? s = "0" + s : s;
        that.setData({
          qgdjs_jo: { day: d, hour: h, min: m, sec: s }
        })
      }, 1000)
    })

  },


  //
  //
  // //绑定分享功能
  // onShareAppMessage:function (shareOption) {
  //
  //   console.log(shareOption)
  //   let dyUser = tt.getStorageSync('dyUser')
  //   let goodsId = tt.getStorageSync('g_id');
  //
  //   console.log(dyUser, "dyUser");
  //   if (dyUser.id == "") {
  //     tt.showToast({
  //       title: "拍抖音绑定失败",
  //       duration: 2000,
  //       success(res) {
  //         console.log(`${res}`);
  //       },
  //       fail(res) {
  //         console.log(`showToast调用失败`);
  //       },
  //     });
  //     return;
  //   }
  //   let shareUserId = dyUser.uid;
  //   let mId = dyUser.id;
  //   switch (shareOption.channel) {
  //     case 'video':
  //       let sharePath = 'shareUserId=' + shareUserId + '&mId=' + mId+ 'goodsId='+ goodsId;
  //       // let query = {
  //       //   shareUserId: shareUserId,
  //       //   mid: mId
  //       // }
  //
  //       // console.log(JSON.stringify(query),"sharePath")
  //       // console.log(query, "video")
  //       return {
  //         title: "金牛贺岁！牛牛牛！",
  //         query: sharePath,
  //       }
  //
  //       break;
  //     case 'qrcode':
  //
  //       break;
  //     default:
  //
  //       break;
  //   }
  // }
  //
});
